package com.crimsec.utils.dinfo.impl;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;

/**
 *
 * @author moroz
 */
public class AbstractRenderer {
    
    protected boolean setupIt=false;
    
    public static <K,V> SortedMap<K,V>  getHeadMapForCount(SortedMap<K, V> map, int count) {
        if (count <= 0) {
            return map.headMap(map.firstKey());
        } else if (count >= map.size()) {
            return map;
        } else {
            K key = null;
            int i = 1;
            for (K k : map.keySet()) {
                if (i == count + 1) {//high endpoint (exclusive) of the keys in the returned map
                    key = k;
                }
                i++;
            }
            return map.headMap(key);
        }
        
    }
    
    public void check(Map files, File dir, String name) throws IOException {
        
        if (!this.setupIt) {
            throw new IOException("setup must be called first");
        }
        
        if (!((files != null) && (!files.isEmpty()))) {
            throw new IOException("files map must be not empty");
        }
        if (!((dir != null) && (dir.isDirectory()))) {
            throw new IOException("initial must be a directory");
        }
        if (!((name != null) && (!name.isEmpty()))) {
            throw new IOException("render name must be not empty, but name=" + name);
        }
        
    }
}
